package 品牌管理;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class 查询一个品牌 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的品牌id：");
        int id4Query = sc.nextInt();
//        1.加载驱动com.mysql.jdbc.Driver
//        创建list存放品牌
//        泛型
        ArrayList<brand> brandList =  new ArrayList<brand>();
        brand b =null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
//        2.使用驱动管理器（DriverManager）连接数据库              ？是后面要加参数&支持中文&xx连接关闭&服务器通用时区
            String url = "jdbc:mysql://10.4.43.109/db_jdbc_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
            String user = "root";
            String pass = "000000";
            conn = DriverManager.getConnection(url, user, pass);
//        3.书写sql语句并执行
            String sql = "SELECT * FROM t_brand WHERE id = ?";
//        4.创建执行SQL的对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id4Query);
            rs = pstmt.executeQuery();
//        5.从rs中取出结果
            while (rs.next()){
//                rs.getString(1);
                int id = rs.getInt("id");
                String brandName = rs.getString("brand_name");
                String companyname = rs.getString("company_name");
                int ordered = rs.getInt("ordered");
                String description = rs.getString("description");
                int status = rs.getInt("status");
//                System.out.println(id+brandName+companyname+ordered+description+status);
//                封装
                b = new brand(id,brandName,companyname,
                        ordered,description,status);

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //        6.关闭连接
            try {
                rs.close();
                pstmt.close();
                conn.close();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("所查品牌是：");
        System.out.println(b);

    }
}
